/***********************************************************************************************//**
 * \file   batt.c
 * \brief  Battery Service
 ***************************************************************************************************
 * <b> (C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>
 ***************************************************************************************************
 * This file is licensed under the Silabs License Agreement. See the file
 * "Silabs_License_Agreement.txt" for details. Before using this software for
 * any purpose, you must agree to the terms of that agreement.
 **************************************************************************************************/

#include CONFIGURATION_HEADER
#include CALLBACKS_HEADER

/* standard library headers */
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>

/* BG stack headers */
#include "bg_types.h"
#include "gatt_db.h"
#ifdef HOST
#include "gecko_bglib.h"
#else /* !HOST */
#include "native_gecko.h"
#endif /* !HOST */
#include "infrastructure.h"

/* plugin headers */
#include "app_hw.h"
#include "event_control.h"
#include "connection.h"

/* Own header*/
#include "batt.h"

/***********************************************************************************************//**
 * @addtogroup Features
 * @{
 **************************************************************************************************/

/***********************************************************************************************//**
 * @addtogroup batt
 * @{
 **************************************************************************************************/

/***************************************************************************************************
  Local Macros and Definitions
***************************************************************************************************/

/** Battery measurement period in ms. */
#define BATT_IND_TIMEOUT                SILABS_AF_PLUGIN_BATT_NOTIFICATION_INT

/** Indicates currently there is no active connection using this service. */
#define BATT_NO_CONNECTION                   0xFF

/***************************************************************************************************
 Local Type Definitions
 **************************************************************************************************/

/***************************************************************************************************
 Local Variables
 **************************************************************************************************/

static uint8 battBatteryLevel; /* Battery Level */

/***************************************************************************************************
 Static Function Declarations
 **************************************************************************************************/

/***************************************************************************************************
 Public Variable Definitions
 **************************************************************************************************/
EmberEventControl battTimerEvt = { EVENT_INACTIVE, 0 };

/***************************************************************************************************
 Public Function Definitions
 **************************************************************************************************/

void battInit(void)
{
  eventControlSetInactive(battTimerEvt); /* Make sure battery level measurement is stopped */
}

void battCharStatusChange(uint8_t connection, uint16_t clientConfig)
{
  /* if the new value of CCC is not 0 (either indication or notification enabled)
   *  start battery level measurement */
  if (clientConfig)
  {
    battMeasure(); /* make an initial measurement */
//    emberAfPluginBattIndicationsOnCallback(); /* call callback */
  }
  else
  {
    eventControlSetInactive(battTimerEvt); /* Stop measurement */
  }
}


void battMeasure(void)
{

  /* Update battery level based on battery level sensor */
  battBatteryLevel = appHwReadBatteryLevel();

  /* Send notification */
  gecko_cmd_gatt_server_send_characteristic_notification(conGetConnectionId(),
		  gattdb_batt_measurement, sizeof(battBatteryLevel), &battBatteryLevel);

//  emberAfPluginBattMeasAvailableCallback(battBatteryLevel); /* call callback */

  eventControlSetDelayMS(&battTimerEvt, BATT_IND_TIMEOUT); /* Restart timer */
}

void battRead(void)
{
  /* Update battery level based on battery level sensor */
  battBatteryLevel = appHwReadBatteryLevel();
  /* Send response to read request */
  gecko_cmd_gatt_server_send_user_read_response(conGetConnectionId(), gattdb_batt_measurement, 0,
		  sizeof(battBatteryLevel), &battBatteryLevel);

}


/** @} (end addtogroup batt) */
/** @} (end addtogroup Features) */

